<template>
  <div id="app-container" style="margin: 10px">

    <!--搜索input框 搜索按钮 重置按钮-->
    <el-form :inline="true" style="margin-bottom: -10px">
      <el-form-item label="操作人员名称:">
        <el-input v-model="queryLogDTO.userName" size="small" placeholder="请输入人员名称"/>
      </el-form-item>

      <el-form-item label="操作状态" prop="status" clearable>
        <el-select v-model="queryLogDTO.status" size="small" clearable placeholder="请选择">
          <el-option :value="0" label="成功"/>
          <el-option :value="1" label="失败"/>
        </el-select>
      </el-form-item>

      <el-form-item label="操作时间:">
        <el-date-picker
          v-model="queryLogDTO.createTime"
          type="datetime"
          placeholder="选择操作时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          size="small"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="userPageListQuery()">搜索</el-button>
        <el-button plain icon="el-icon-refresh" size="small" style="margin-left: 0" @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>

    <!--新增、修改、删除、导出按钮-->
    <el-form :inline="true">
      <el-button type="danger" plain icon="el-icon-delete" size="small" @click="deleteBySelect">批量删除</el-button>
      <el-button type="warning" plain icon="el-icon-download" size="small" style="margin-left: 0" @click="handleExport">导出数据</el-button>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="UserList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"/>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (currentPage-1)*pageSize + scope.$index +1 }}
        </template>
      </el-table-column>

      <el-table-column
        prop="userName"
        label="操作人员"
        width="150"
        align="center"/>

      <el-table-column
        prop="userIp"
        label="操作地址"
        width="150"
        align="center"/>

      <el-table-column
        prop="location"
        label="操作地点"
        width="150"
        align="center"/>

      <el-table-column label="操作状态" prop="status" width="150" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == '0'">成功</el-tag>
          <el-tag v-if="scope.row.status == '1'" type="danger">失败</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="type"
        label="操作类型"
        width="150"
        align="center"/>

      <el-table-column
        align="center"
        prop="createTime"
        label="操作时间"
        width="200"
        show-overflow-tooltip/>

      <el-table-column align="center" fixed="right" width="150" label="操作">

        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"

          >详细</el-button>
        </template>

      </el-table-column>
    </el-table>

    <!-- 操作日志详细 -->
    <el-dialog :visible.sync="open" title="操作日志详细" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块：">{{ form.title }} / {{ typeFormat(form) }}</el-form-item>
            <el-form-item
              label="登录信息："
            >{{ form.userName }} / {{ form.userIp }} / {{ form.location }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求方式：">{{ form.type }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作方法：">{{ form.method }}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="操作状态：">
              <div v-if="form.status === 0">成功</div>
              <div v-else-if="form.status === 1">失败</div>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="操作时间：">{{ form.createTime }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>

    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>

  </div>
</template>
<script>
import log from '../../api/log'
import fileDownload from 'js-file-download'
import axios from 'axios'
export default {
  data() {
    return {
      queryLogDTO: {
      },
      UserList: [],
      currentPage: 1,
      pageSize: 10,
      total: 1,
      ids: [],
      open: false,
      form: {},
      businessType: undefined,
      multipleSelection: ['']
    }
  },
  created() {
    this.initDate()
  },
  methods: {
    userPageListQuery() {
      this.initDate()
    },
    resetData() {
      this.queryLogDTO = {}
      this.initDate()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.initDate()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initDate()
    },
    handleDelete(id) {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        this.ids.push(id)
        log.deleteUser(this.ids).then(response => {
          this.$message.success('删除成功')
          this.initDate()
          this.ids = []
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true
      this.form = row
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = []
      for (let i = 0; i < val.length; i++) {
        if (this.multipleSelection.indexOf(val[i].id) === -1) {
          this.multipleSelection.push(val[i].id)
        }
      }
      console.log(this.multipleSelection)
    },
    deleteBySelect() {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        log.deleteUser(this.multipleSelection).then(response => {
          this.$message.success('删除成功')
          this.multipleSelection = []
          this.initDate()
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    initDate() {
      log.listPage(this.currentPage, this.pageSize, this.queryLogDTO).then(response => {
        this.UserList = response.data.data
        this.total = response.data.total
      })
    },

    typeFormat(row, column) {
      return this.selectDictLabel(this.queryLogDTO.type, row.businessType)
    },

    handleExport() {
      console.log('===导出数据===')
      axios({
        method: 'post',
        url: 'http://localhost:8080/log/export',
        responseType: 'blob',
        data: this.queryLogDTO
      }).then(res => {
        if (res && res.data) {
          fileDownload(res.data, '操作日志.xlsx')
        }
      })
    }
  }
}
</script>
